<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<form id="commonconfig_form" action="" method="post"  class="form-horizontal">
    <input type="hidden" name="selectType" id="selectType" value="${selectType}"/>
    <input type="hidden" id="sType" value="${sType}"/>
    <input type="hidden" name="keyId" id="keyId" value="${keyId}"/>
    <input type="hidden" name="allMans" id="allMans" value="<c:if test='${commonConfig.allmans==true}'>1</c:if><c:if test='${commonConfig.allmans==false
    }'>0</c:if>" />

    <div class="form-group">
        <div class="col-md-1">
        </div>
        <div class="col-md-7">
            <div class="checkbox">
                <input type="checkbox"  name="type" value="1" id="type1" class="magic-checkbox">
                <label for="type1">所有人</label>
                <input type="checkbox"  name="type" value="2" id="type2" class="magic-checkbox">
                <label for="type2">部门</label>
                <input type="checkbox"  name="type" value="3" id="type3" class="magic-checkbox">
                <label for="type3">岗位</label>
                <input type="checkbox"  name="type" value="4" id="type4" class="magic-checkbox">
                <label for="type4">职权</label>
                <input type="checkbox"  name="type" value="5" id="type5" class="magic-checkbox">
                <label for="type5">群组</label>
                <input type="checkbox"  name="type" value="6" id="type6" class="magic-checkbox">
                <label for="type6">域名</label>

            </div>
        </div>
    </div>
    <div class="form-group"  id="type-2" style="display:none">
        <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-depart" style="color:#4FA5D0">部门</a>
        </label>
        <div class="col-md-6">
            <input  disabled type="text" id="departName" class="form-control"
                    value="<c:forEach items="${commonConfig.departmentSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="departId" name="departId"
                   value="<c:forEach items="${commonConfig.departmentSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
        </div>
    </div>

    <div class="form-group" id="type-3" style="display:none">
        <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-post" style="color:#4FA5D0">岗位</a>
        </label>
        <div class="col-md-6">
            <input disabled  type="text" id="postName" class="form-control"
                   value="<c:forEach items="${commonConfig.postSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="postId" name="postId"
                   value="<c:forEach items="${commonConfig.postSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
        </div>
    </div>
    <div class="form-group" id="type-4" style="display:none">
        <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-power" style="color:#4FA5D0">职权</a>
        </label>
        <div class="col-md-6">
            <input disabled  type="text" id="powerName"  class="form-control"
                   value="<c:forEach items="${commonConfig.powerSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.department.name}-${list.post.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>" />
            <input type="hidden"  id="powerId" name="powerId"
                   value="<c:forEach items="${commonConfig.powerSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
        </div>
    </div>

    <div class="form-group" id="type-5" style="display:none">
        <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-group" style="color:#4FA5D0">群组</a>
        </label>
        <div class="col-md-6">
            <input disabled type="text" id="groupName"  class="form-control"
                   value="<c:forEach items="${commonConfig.groupSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="groupId" name="groupId"
                   value="<c:forEach items="${commonConfig.groupSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
        </div>
    </div>
    <div class="form-group"  id="type-6" style="display:none">
        <label class="col-md-2 control-label">
            域名
        </label>
        <div class="col-md-6">
            <select class="form-control" name="variable" id="variable">
                <option value="">--请选择--</option>
                <c:forEach items="${fieldNames}" varStatus="i" var="list">
                    <option  value="${list}"  <c:if test='${list==commonConfig.variable}'>selected</c:if>>${list}</option>
                </c:forEach>
            </select>
        </div>
    </div>
</form>

<script>

    $("#commonconfig_form input[name='type']").click(function(){
        var id = $(this).val();
        if($(this).prop("checked")){
            $("#commonconfig_form #type-"+id).show();
            if(id == "1"){
                $('#allMans').val(1);
            }
        }else{
            $("#commonconfig_form #type-"+id).hide();
            if(id == "1"){
                $('#allMans').val(0);
            }
        }
    });

    // 岗位选择框
    function fn_post_dialog_select(nameField,idField) {

        __open_dialog_select('选择岗位', '<%=path%>/main/post/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_post_select(callback);
        });
    }
    // 部门选择框
    function fn_departments_dialog_select(isMulti,nameField,idField) {

        __open_dialog_select('选择部门', '<%=path%>/main/department/dialog?multi='+isMulti+'&deptIds='+ $('#'+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_departments_select(callback);
        });
    }
    // 职权选择框
    function fn_power_dialog_select(nameField,idField) {

        __open_dialog_select('选择职权', '<%=path%>/main/power/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_power_select(callback);
        });
    }

    // 群组选择框
    function fn_group_dialog_select(nameField,idField) {

        __open_dialog_select('选择群组', '<%=path%>/main/group/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_group_select(callback);
        });
    }


    $("a[key='btn-choose-group']").unbind("click").bind("click", function(){fn_group_dialog_select("groupName","groupId")} );
    $("a[key='btn-choose-depart']").unbind("click").bind("click", function(){fn_departments_dialog_select("y","departName","departId")});
    $("a[key='btn-choose-post']").unbind("click").bind("click", function(){fn_post_dialog_select("postName","postId")});
    $("a[key='btn-choose-power']").unbind("click").bind("click",  function(){fn_power_dialog_select("powerName","powerId")});

    function fn_commoncfg_input_save(callback) {
        __form_save('commonconfig_form', '<%=path%>/main/commoncfg/save', function(data){
            if(data.state == 1){
                callback(data);
            } else {
                callback();
            }
        });
        /*
        __confirm_dialog(null, '确定提交么?',
            function () {

            },
            function () {
                callback();
            }
        );*/
    }


    $(function(){
        var sType=$("input#sType").val();
        $("input[name='type']").each(function(i,v){
            var optVal = $(v).val();
            if(sType != "" && sType.indexOf(optVal) != -1){
                $(v).prop("checked",true);
                $("div.modal-body #type-"+optVal).show();
            }
        });
    });
</script>